\section{Tables and Arrays}

In this video, we're going to take a look at two very similar and fairly common constructions in \LaTeX: tables and arrays. Both tables and arrays are structured environments in which there are well-defined rows and columns. The primary difference is that arrays are mathematical objects whereas tables are text-based. We will first focus our attention on tables to understand how these environments work, and then expand to discussing arrays.

There is an unfortunate naming convention that \LaTeX{} has, which is that there is a table environment that is used in academic writing, and then there is a tabular environment which is usually used to make a table. To make things even more confusing, within the table environment, the table is actually constructed using a tabular environment. In most circumstances, when people talk about making a table in \LaTeX{} they are referring to a tabular environment and not a table environment.

\begin{center}
  \fbox{\includegraphics[width=0.7\linewidth]{img-4-1}}
\end{center}

The tabular environment requires you to define the number of columns and their alignments. This is perhaps best understood through an example. For this table, we will use \texttt{lcr} to define our columns. What this means is that we will have three columns, where the first is left justified, the second is centered, and the third is right justified.

To fill in the values in the table, we simply work in rows from left to right. Each time we want to move over to the next cell, we use an ampersand. And when we get to the end of a row, we use a double backslash to start a new row. If you forget the double backslash, \LaTeX{} will complain when you try to compile your document. Here is the output.

Notice that this fits the structure we created. There are three columns. The first is left justified, the next is centered, and the last one is right justified, and the contents of each row matches with what we stated. And this is the basic way to make a table. The extra spaces to make the columns line up in the code aren't necessary, but they make the code more readable. 

From here, we can start to get a little fancier with the table. If we wanted to add vertical lines to separate the columns, we would put vertical bars into the alignment definition. We can also get double lines by putting two vertical bars between the columns. The vertical bars will only be drawn if you have cells that require them. If you end a row early without creating blank cells, the vertical lines will have gaps in them.

To create a horizontal line through the table, use the \verb|\hline| command at the start of the row that's directly below where you want the line to be. This means that to create a line across the top of the table, you need to call \verb|\hline| at the start of the first row of data. To create a line at the bottom of the table, you need to create one row at the end that's empty except for the \verb|\hline| command. A double line can be created using two \verb|\hline| commands.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-4-2}}
\end{center}

If you want to get fancier with your horizontal lines, you will have to use the \href{https://ctan.org/pkg/hhline}{\texttt{hhline}} package. With this package, instead of calling \verb|\hline| you will call \verb|\hhline{}| and then specify how exactly you want the line to be drawn. A dash will give you a single line, an equal sign will give you a double line, and a tilde will create a gap. You will also need to define the vertical bars to avoid having small gaps in them. This package has a few more options, but you'll have to read through the documentation for yourself to learn about those things.

One thing you might have noticed about the tabular environment is that \LaTeX{} decides how wide to make the columns. This is fine in most situations, but there are times when you want to control the column width. For example, if you have lots of text to put in a cell, the fixed column width will cause the text to wrap instead of stretching out the column to fit the text. One solution for this uses the \href{https://ctan.org/pkg/array}{\texttt{array}} package and some custom column types. This is a situation where the explanation for what the code is doing is more complex than is worth understanding at this time, so I'm just doing to describe how to use it. If you want to know where this code came from, the \href{https://tex.stackexchange.com/questions/12703/how-to-create-fixed-width-table-columns-with-text-raggedright-centered-raggedlef}{link} is in the description.

\begin{center}
  \fbox{\includegraphics[width=0.95\linewidth]{img-4-3}}
\end{center}

Once you've typed in these commands, all you need to do to control the width of a column is to use a capital letter in place of the lower case letter and enclose the column width inside of curly brackets. You can use several different types of units, but I usually use millimeters or centimeters. You probably won't really need the units of points, exs, and ems until you start getting deeper in \LaTeX.

Before we transition into typesetting mathematical arrays, it's worth noting that there are several packages out there that can be used to create even nicer looking tables. Perhaps the most commonly referenced package is the \href{https://ctan.org/pkg/booktabs}{\texttt{booktabs}} package, but you will also see examples using the \href{https://ctan.org/pkg/tabularx}{\texttt{tabularx}} package and the \href{https://ctan.org/pkg/colortbl}{\texttt{colortbl}} package. There's also the \href{https://ctan.org/pkg/longtable}{\texttt{longtable}} package for when you have a table that is split across multiple pages. I've included a \href{https://tex.stackexchange.com/questions/112343/beautiful-table-samples}{link} to a discussion of tables in the description and the transcript as a starting point, but you will need to explore these packages on your own if you want to learn more about them.

Arrays work on the exact same principle as tables, except that arrays must be called inside math mode. The individual cells are assumed to be in math mode, so all your symbols, subscripts, and superscripts will be available to you. If you wanted to have vertical or horizontal bars, you can create them in the exact same way.

A common use of arrays is to create matrices. Matrices are usually denoted by parentheses or brackets around the array. These can be created manually by using the \verb|\left| and \verb|\right| commands and your symbol of choice. However, the brackets and the spacing for arrays built this way can sometimes be larger than necessary.

\begin{center}
  \fbox{\includegraphics[width=0.8\linewidth]{img-4-4}}
\end{center}

Fortunately, the \href{https://ctan.org/pkg/amsmath}{\texttt{amsmath}} package has commands that makes matrices easier. With their matrix environments \texttt{pmartrix} and \texttt{bmatrix}, you do not need to specify the columns in advance. You simply decide what type of symbols you want around the outside and jump straight into defining the content of the cells. The matrix command will set up the columns by itself and everything will automatically be centered.

\begin{center}
  \fbox{\includegraphics[width=0.7\linewidth]{img-4-5}}
\end{center}

There are a few special dot symbols that are helpful in the context of matrices. The command \verb|\vdots| will make a vertical ellipsis, the command \verb|\cdots| will make a horizontal ellipsis at about the middle of the line, and the command \verb|\ddots| will make diagonally downward dots. It turns out that you can't make diagonally upward dots without including a separate package. There are a few that do it, but I prefer the \href{https://ctan.org/pkg/mathdots}{\texttt{mathdots}} package, which has the command \verb|\iddots|. There's an added benefit to using the \href{https://ctan.org/pkg/mathdots}{\texttt{mathdots}} package, which is that it will also make other dots look nicer if you're using different font sizes.

\begin{center}
  \fbox{\includegraphics[width=0.65\linewidth]{img-4-6}}
\end{center}

One last trick for both tables and arrays is merging columns and rows together. For columns, there is a command called \verb|\multicolumn{}{}{}| that accomplishes this. Here is an example to demonstrate how it's used. The first brackets are used to indicate the number of columns being merged, the second brackets are used to define the alignment of the new column and any vertical lines you want to have around it, and the last brackets are used for the actual contents of the cell.

\begin{center}
  \fbox{\includegraphics[width=0.7\linewidth]{img-4-7}}
\end{center}

Multiple rows can be merged together, but it requires the \href{https://ctan.org/pkg/multirow}{\texttt{multirow}} package. The command for this is \verb|\multirow[]{}{}{}|. The square brackets are for an optional command to vertically align the text at the top, the center, or at the bottom. If you leave this out, it will default to center alignment. The first curly brackets indicate how many rows you want merged. The next curly brackets will declare the width of the column. As long as you're not doing anything too fancy, you should be able to use \verb|*| here. But you can also use a measurement to create a specific width. The last brackets represent the contents of the cell. You will need to use the \verb|\multirow| command on the top cell that will be merged, and on lower rows you will need to create a blank cell where the merged cell is located.

\begin{center}
  \fbox{\includegraphics[width=0.7\linewidth]{img-4-8}}
\end{center}

Here is an example that combines several of the commands in this section together. It's probably rare that you would try to put this many things together all at once, but it's a good exercise to work through the commands and make sure that you fully understand what's happening.

The next video will discuss the notation used in calculus.